// Create the canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);


// Character and monster Stats // class // action


var state = "startmenu"; // state = "startmenu"; state = "exploration"; state = "fight";
var then;
var cursor = new Image();
var sprite = new Image();
var grass = new Image();
var monstersSprites = new Image();
var isSelected = false;
var isSelecting = false;

var map = [];
var party = [];
var inventory = [];

var monsters = {
    goblin: new Monster("Goblin", 50, 0, 20, 20, 5, 20, 25, 15, 5, 4, 5, ["goblin punch", "restore"], ""),
    warg : new Monster("Warg", 50, 0, 20, 20, 5, 20, 25, 15, 5, 4, 5, ["goblin punch", "restore"], ""),
    Chaos : new Monster("Chaos",10000 , 0 ,55 , 40 ,20 , 30 ,50,300 ,500 ,20 ,2 , [] , "")
};
var monstersBattle = [];
var allSpells = {
    cure : new Spell("cure",50)
};

var targetingMonster = true;
var targetingAlly = false;
var monsterTarget = 1;
var allyTarget = 1;
var startUp = true;
var turn = 1;
var fightMenu = 1;
var statusMenu = 1;
var selectCharacterMenu = 1;
var selectJobForParty = 1;
var startingMenu = 1;



function Character (lvl ,name,hp , mp , strength , agility  , intelligence , stamina , attack ,defense,magicDefense, evasion ,luck , weapon ,armor ,animation ,image){
    this.lvl = lvl;
    this.name = name;
    this.hp = hp; // point de vie
    this.mp = mp; // point de magic
    this.strength = strength;
    this.agility  = agility ;
    this.intelligence = intelligence;
    this.stamina = stamina;
    this.attack = attack;
    this.defense = defense;
    this.magicDefense = magicDefense;
    this.evasion = evasion;
    this.luck = luck;
    this.weapon = weapon;
    this.armor = armor;
    this.animation = animation || "";
    this.spells = [];
    this.menu = [];
    this.image = image;
    this.turn = false;

    Character.prototype.useAttack = function(damage,target){
        target.hp -= damage;
    };
    Character.prototype.useMagic = function(damage,target){
        target.hp -= damage;
    };
    Character.prototype.useItem = function(target , itemQuantity , itemPotency){

    };
    Character.prototype.fuire = function(){


    }

}

function Knight(){
    Knight.prototype.protect = function(target){}
    this.menu = ["attack" , "protect" , "item"];
}

function Ninja(){
    Ninja.prototype.throws = function(){}
    this.menu = ["attack" , "throws" , "item"];
}
function Master(){
    Master.prototype.karma = function(){}
    this.menu = ["attack" , "karma" , "item"];
}
function RedWizard(){
    Character.call(this.spells);
    this.menu = ["attack" , "red magic" , "item"];
}
function WhiteWizard(){
    this.menu = ["attack" , "white magic" , "item"];
}
function BlackWizard(){
    this.menu = ["attack" , "black magic" , "item"];
}




//                              lvl,name,hp, mp,str,agi,int,sta , att ,def,mdf , eva, lck , weapon,     armor
Knight.prototype = new Character(50,"Knight",6013,60,45,34 , 21 , 42 ,50, 60 , 20 , 20 , 10 , "Long sword" , "Iron armor","",sprite);
Ninja.prototype = new Character(50,"Ninja",4965,0,45,34 , 21 , 42 ,50 , 60 , 20 , 20 , 10 , "dagger" , "Iron armor","",sprite);
Master.prototype = new Character(50,"Master",5310,0,45,34 , 21 , 42 ,50 , 60 , 20 , 20 , 10 , "iron knuckles" , "Iron armor","",sprite);
BlackWizard.prototype = new Character(50,"",3919,280,45,34 , 21  , 42 ,50, 60 , 20 , 20 , 10 , "staff" , "Iron armor","",sprite);
WhiteWizard.prototype = new Character(50,"",4612,260,45,34 , 21 , 42 ,50, 60 , 20 , 20 , 10 , "staff" , "Iron armor","",sprite);
RedWizard.prototype = new Character(50,"",5012,200,45,34 , 21 , 42 ,50, 60 , 20 , 20 , 10 , "Long sword" , "Iron armor","",sprite);

function Monster(name ,hp,mp,strength,agility,intelligence,stamina,attack,defense,magicDefense,evasion,luck,spells,image){
   // the name of the monster
    this.name = name;
    // Life point / hit point
    this.hp = hp;
    // magic point
    this.mp = mp;
    // Normal Stats
    this.strength = strength;
    this.agility  = agility ;
    this.intelligence = intelligence;
    this.stamina = stamina;
    this.attack = attack;
    this.defense = defense;
    this.magicDefense = magicDefense;
    this.evasion = evasion;
    this.luck = luck;
    // the monsters spells , can be empty
    this.spells = spells || [];
    // the monster image , no need to be animated they won't move anyway.
    this.image = image;

    Monster.prototype.useAttack = function(damage,target){
        target.hp -= damage;
    };

}

function Goblin(){


}

function Warg(){

}

function Chaos(){

}

Goblin.prototype = new Monster("Goblin", 250, 0, 20, 20, 5, 20, 25, 15, 5, 4, 5, ["goblin punch", "restore"], "");
Warg.prototype = new Monster("Warg", 250, 0, 20, 20, 5, 20, 25, 15, 5, 4, 5, ["goblin punch", "restore"], "");
Chaos.prototype = new Monster("Chaos",10000 , 0 ,55 , 40 ,20 , 30 ,50,300 ,500 ,20 ,2 , [] , "");


function Spell(name ,potency, animation){

    this.name = name;
    this.potency = potency;
    this.animation = animation;

}

function Item(name , description , potency , type,quantity , isThrowable){

    this.name = name;
    this.description = description;
    this.potency = potency;
    this.type = type;   //type (healing , cure Status , damage )
    this.quantity = quantity;
    this.isThrowable = isThrowable; // For the ninja class
}

function Leader (positionX,positionY){
    this.positionX = positionX;
    this.positionY = positionY;
    //this.movementPositionLeft = movementPositionLeft;
    //this.movementPositionRight = movementPositionRight;
    //this.movementPositionUp = movementPositionUp;
    //this.movementPositionDown = movementPositionDown;
}

var leader = new Leader(20,20);

var loadImage = function(){
    cursor.onload = function() {

    };
    cursor.src = "../images/Cursor.png";

    sprite.onload = function() {

    };
    sprite.src = "../images/CharacterSprite.png";

    //grass.onload = function(){
    //
    //};
    //grass.src = "../images/grass.png";

    monstersSprites.onload = function(){

    };
    monstersSprites = "../images/monsters.png";


};

window.onload = function()
{
    // "Hurt" sound effect
    //hurt = new Howl({
    //    //urls: ['audio/hurt.mp3', 'audio/hurt.ogg', 'audio/hurt.wav']
    //});

// Let's play this game!
//    reset();
//    then = Date.now();
//    loadmap(level,15,32,32,0,0);
//
    var knight = new Knight;
    console.log(knight.agility);
    loadImage();
    party = [new Knight(),new Ninja(), new BlackWizard() , new WhiteWizard()];

    console.log("lvl " + party[0].lvl +"name " + party[0].name +"hp " + party[0].hp +"mp " + party[0].mp +"strength " + party[0].strength +"agility " + party[0].agility +"intelligence " + party[0].intelligence +"stamina " + party[0].stamina +"attack " + party[0].attack +"defense " + party[0].defense + "image : " + party[0].image);
    console.log("intel" + party[1].intelligence);



    main();
};

// Wrap-up for the Request Anim frame API, with setTimeout fallback for old browsers.
window.requestAnimFrame = (function (callback)
{
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function (callback)
        {
            window.setTimeout(callback, 1000 / 60);
        };
})();

// Handle keyboard controls
//var keysDown = {};
//var keysUp = {};
//addEventListener("keydown", function (e) {
//    delete keysUp[e.keyCode];
//    keysDown[e.keyCode] = true;
//
//}, false);
//
//addEventListener("keyup", function (e) {
//    delete keysDown[e.keyCode];
//    keysUp[e.keyCode] = true
//}, false);

var Key = {
    _pressed: {},

    LEFT: 37,
    UP: 38,
    RIGHT: 39,
    DOWN: 40,
    ENTER: 13,

    isDown: function(keyCode) {
        return this._pressed[keyCode];
    },

    onKeydown: function(event) {
        this._pressed[event.keyCode] = true;
    },

    onKeyup: function(event) {
        delete this._pressed[event.keyCode];
    }
};

var prepareBattle = function(){
    var NumberOfEnnemies = (Math.random() * 6);
    for(var i = 0 ; i < NumberOfEnnemies ; i++){
        var getRandomMonster = (Math.random() * monsters.length);
        monstersBattle.push( monsters[getRandomMonster]);

    }

};

window.addEventListener('keyup', function(event) { Key.onKeyup(event); }, false);
window.addEventListener('keydown', function(event) { Key.onKeydown(event); }, false);

var showCharacterStatus = function(){
    ctx.fillStyle = "black";
    ctx.strokeRect(10, 100, 180, 80);

    // draw font in red
    ctx.fillStyle = "white";
    ctx.font = "20pt sans-serif";
    ctx.fillText(party[0].name, 5, 100);
    ctx.strokeText("hp : "+party[0], 5, 130);

};

var castSpell = function(spellName){
    for(var x in allSpells){
        if(x.name == spellName){
            return x.animation;

        }
    }

};


// Update game objects
var update = function () {
// Modified to use WASD instead of arrow keys

    if(state == "startmenu"){
        if (Key.isDown(Key.UP)) { // Player holding up
            if(startingMenu > 1){
                startingMenu--;
            }

        }
        if (Key.isDown(Key.DOWN)) { // Player holding down
            if(startingMenu < 3){
                startingMenu++;
            }
        }
        if(Key.isDown(Key.ENTER)){ // Player pressed enter
            state = "characterSelection";
        }
    }

    if(state == "characterSelection"){
        console.log(selectCharacterMenu);
        console.log(isSelected);
        if (Key.isDown(Key.UP)) { // Player holding up
            if(!isSelected) {
                if (selectCharacterMenu > 1) {
                    selectCharacterMenu--;
                }
            }
        }
        if (Key.isDown(Key.DOWN)) { // Player holding down
            if(!isSelected) {
                if (selectCharacterMenu < 5) {
                    selectCharacterMenu++;
                }
            }
        }
        if (Key.isDown(Key.LEFT)) { // Player holding left
            if(isSelected){
                if(selectJobForParty > 1){
                    selectJobForParty--;
                }
            }
        }
        if (Key.isDown(Key.RIGHT)) { // Player holding right
            if(isSelected){
                if(selectJobForParty < 6){
                    selectJobForParty++;
                }
            }
        }
        if(Key.isDown(Key.ENTER)){ // Player pressed enter
            if(selectCharacterMenu<5){
                if(!isSelected){

                    isSelected = true;
                    console.log(isSelected);
                }
            }
            else if(selectCharacterMenu == 5) {
                state = "exploration";
            }
            if(isSelected){
                var characterToAdd;

                switch (selectJobForParty) {

                    case 1:
                        characterToAdd = new Knight();
                        break;
                    case 2:
                        characterToAdd = new Ninja();
                        break;
                    case 3:
                        characterToAdd = new Master();
                        break;
                    case 4:
                        characterToAdd = new BlackWizard();
                        break;
                    case 5:
                        characterToAdd = new WhiteWizard();
                        break;
                    case 6:
                        characterToAdd = new RedWizard();
                        break;
                }
                party[selectCharacterMenu-1] = characterToAdd;
                selectCharacterMenu = 1;
                selectJobForParty = 1;
                characterToAdd = 1;
                isSelected = false;
                console.log("Je suis ici");
            }
        }
    }

    if(state == "exploration"){
        if (Key.isDown(Key.UP)) { // Player holding up
            if(!(leader.positionY-4 <= 0)){
                leader.positionY-=4;
            }

        }
        if (Key.isDown(Key.DOWN)) { // Player holding down
            if(!(leader.positionY+4 >= canvas.height-20)){
                leader.positionY+=4;
            }

        }
        if (Key.isDown(Key.LEFT)) { // Player holding left
            if(!(leader.positionX-4 <= 0)) {
                leader.positionX -= 4;
            }
        }
        if (Key.isDown(Key.RIGHT)) { // Player holding right
            if(!(leader.positionX+4 > canvas.width -32)) {
                leader.positionX += 4;
            }
        }
        console.log(leader.positionY);
        console.log(leader.positionX);
        console.log(canvas.width);
        console.log(canvas.height);
    }

    if(state == "fight") {

        if(startUp == true){
            generateRandomEnnemies();
            startUp = false;
        }
        console.log(monstersBattle.length);
        //console.log(monstersBattle[0].hp );
        //console.log(monstersBattle[1].hp );
        console.log(" monsterTarget : " + monsterTarget);

        if (Key.isDown(Key.UP)) { // Player holding up

            if(!isSelecting){
                if(fightMenu > 1) {
                    fightMenu--;
                }
            }
            else{
                if(targetingMonster) {
                    if (monsterTarget > 1) {
                        monsterTarget--;
                    }
                }
                if(targetingAlly){
                    if (allyTarget > 1) {
                        allyTarget--;
                    }
                }
            }
        }
        if (Key.isDown(Key.DOWN)) { // Player holding down

            if(!isSelecting){
                if(fightMenu < 3) {
                    fightMenu++;
                }
            }
            else{
                if(targetingMonster) {
                    if (monsterTarget < monstersBattle.length) {
                        monsterTarget++;
                    }
                }
                if(targetingAlly){
                    if (allyTarget < 4) {
                        allyTarget++;
                    }
                }
            }
        }
        if (Key.isDown(Key.LEFT)) { // Player holding left
            if(!targetingMonster){
                targetingMonster = true;
                targetingAlly = false;
                monsterTarget = 1;
                allyTarget = 1;

            }
        }
        if (Key.isDown(Key.RIGHT)) { // Player holding right
            if(!targetingAlly){
                targetingMonster = false;
                targetingAlly = true;
                monsterTarget = 1;
                allyTarget = 1;
            }
        }
        if(Key.isDown(Key.ENTER)){
            if(turn < 5) {
                if (fightMenu == 1) {
                    if (!isSelecting) {
                        isSelecting = true;
                    }
                    else {
                        if(targetingMonster) {
                            party[turn - 1].useAttack(party[turn - 1].attack, monstersBattle[monsterTarget-1]);
                        }
                        else if(targetingAlly){
                            party[turn - 1].useAttack(party[turn - 1].attack, monstersBattle[allyTarget-1]);
                        }
                        isSelecting = false;
                        turn++;
                        fightMenu = 1;
                        monsterTarget = 1;
                        allyTarget = 1;

                    }
                }
                else if (fightMenu == 2) {
                    // change party[0] for party[i] when in loop
                    if (party[turn].menu[1] == "protect") {
                    }
                    else if (party[turn].menu[1] == "throws") {
                    }
                    else if (party[turn].menu[1] == "karma") {
                    }
                    else if (party[turn].menu[1] == "black magic") {
                    }
                    else if (party[turn].menu[1] == "white magic") {
                    }
                    else if (party[turn].menu[1] == "red magic") {
                    }
                }
                else if (fightMenu == 3) {
                    party[turn].useItem(inventory);
                }
            }
        };


        if(turn >= 5){
            for(var y = 0 ; y< monstersBattle.length ; y++){
                //setTimeout(function() {
                    monstersBattle[y].useAttack(monstersBattle[y].attack , party[1]);
                //}, 1000);


            }
            turn = 1;

        }
        console.log(party[1]);
        console.log(monstersBattle[0]);

        for(var i = 0; i < monstersBattle.length ; i++){
            if(monstersBattle[i].hp <= 0){
            //    DO SOMETHING
                monstersBattle.splice(i,1);
            }
        }
        if(monstersBattle.length == 0){
            startUp = true;
            state = "exploration";
        }

    }




};

// Draw everything
var render = function () {

    // Clear the screen
    ctx.clearRect(0,0,512,480);


    if(state === "startmenu")
    {
        ctx.fillStyle = "black";
        ctx.clearRect(0,0,512,480);
        ctx.fillRect(0,0,512,480);
        ctx.font = "30px Arial";
        ctx.fillStyle = "blue";
        ctx.fillText("Final Fantasy 1",130,70);
        ctx.fillStyle = "white";
        ctx.fillText("Play!",220,200);
        ctx.fillText("Instruction", 220 ,300);
        ctx.fillText("Credit" , 220 , 400);

        if(startingMenu == 1){

            ctx.drawImage(cursor, 190, 185);
        }
        else if(startingMenu == 2){

            ctx.drawImage(cursor, 190, 285);
        }
        else if(startingMenu == 3){

        ctx.drawImage(cursor, 190, 385);
    }
        //ctx.drawImage(monsterImage,240,100);
    }

    if(state == "characterSelection"){
        ctx.fillStyle = "black";
        ctx.clearRect(0,0,512,480);
        ctx.fillRect(0,0,512,480);
        ctx.font = "30px Arial";
        ctx.fillStyle = "white";
        ctx.fillText("Select your characters",110,40);

        ctx.beginPath();
        ctx.fillStyle = 'brown';
        ctx.rect(350,410, 150, 50);
        ctx.fill();
        ctx.lineWidth = 7;
        ctx.strokeStyle = "gray";
        ctx.stroke();
        ctx.font = "20px Arial";
        ctx.fillStyle = "blue";
        ctx.fillText("Exploration",380,440);


            //              Image source , position X , Y , Grandeur sur image X, Y  ,Destination X , Y  ,Grandeur Sur le Canvas X , Y
            var posX = 0;
            var posY = 20;
        // affichage des 4 prmiers image vertical a droite
        for(var i = 0;  i  < 4; i++) {
            ctx.drawImage(party[i].image,        posX,    i*posY,         20,           20,        415,    (( i+2)*60),              40,            40)

            }


        // affichage des 6 images horizontal a gauche
        for(var i = 0; i < 6; i++) {
            ctx.drawImage(sprite,        posX,    i*posY,         20,           20,       (i+2)*50,    40,              40,            40);
        }

        // test pour le changement de la position du doight en fonction de la selecyion des images
        //if(keysDown == enter)
        if(!isSelected) {
            if (selectCharacterMenu == 1) {

                ctx.drawImage(cursor, 410, 140);
            }
            else if (selectCharacterMenu == 2) {

                ctx.drawImage(cursor, 410, 200);
            }
            else if (selectCharacterMenu == 3) {

                ctx.drawImage(cursor, 410,260);
            }
            else if (selectCharacterMenu == 4) {

                ctx.drawImage(cursor, 410, 320);
            }
            else if (selectCharacterMenu == 5) {

                ctx.drawImage(cursor, 410, 420);
            }
        }
        else
        {
            if (selectJobForParty == 1) {

                ctx.drawImage(cursor, 90, 50);
            }
            else if (selectJobForParty == 2) {

                ctx.drawImage(cursor, 140, 50);
            }
            else if (selectJobForParty == 3) {

                ctx.drawImage(cursor, 200, 50);
            }
            else if (selectJobForParty == 4) {

                ctx.drawImage(cursor, 260, 50);
            }
            else if (selectJobForParty == 5) {

                ctx.drawImage(cursor, 300, 50);
            } else if (selectJobForParty == 6) {

                ctx.drawImage(cursor, 340, 50);
            }
        }

    }

    if(state == "exploration"){

        ctx.fillStyle = "black";
        ctx.clearRect(0,0,512,480);
        ctx.fillRect(0,0,512,480);
        ctx.font = "30px Arial";
        ctx.fillStyle = "white";
        ctx.fillText("exploration",150,40);

        ctx.drawImage(sprite, 0,0, 20,20,leader.positionX,leader.positionY,40, 40);

    }
    if(state == "fight"){
        ctx.fillStyle = "black";
        ctx.clearRect(0,0,512,480);
        ctx.fillRect(0,0,512,480);
        ctx.font = "30px Arial";
        ctx.fillStyle = "white";
        ctx.fillText("Fight",150,40);

        for(var i = 0 ; i < party.length ; i++){
            ctx.drawImage(party[0].image,0,0+(i*21),20,20,395,100+(i*80),40,40);
        }

        for(var m = 0 ; m < monstersBattle.length ; m++){
            ctx.drawImage(monstersSprites , 0, 0 , 20 , 20 ,100 , 50+(i*60) , 30, 30);
        }

        if(turn < 5){
            drawRectFightMenu();
        }
        drawRectEnnemies();
    }

};

// The main game loop
var main = function () {
    var now = Date.now();
    var delta = now - then;

        update();
        render();

    then = now;

    setTimeout(function() {
        requestAnimFrame(main);
        // Drawing code goes here
    }, 1000 / 10);
};


// POUR SIAKA!!!!!!!!!!!!

function drawRectFightMenu(){
    //ctx.rect((canvas.width/2), ((canvas.height/5)*4) ,(canvas.width-(canvas.width/2)) , (canvas.height -((canvas.height/5)*4)));
    //ctx.strokeStyle= "white";
    //ctx.stroke();
    //ctx.font = "20px Arial";
    //
    //


    var x = (canvas.width/2);
    var y = ((canvas.height/5)*4);
    var width = (canvas.width-(canvas.width/2));
    var height = (canvas.height -((canvas.height/5)*4));
    var radius = 5;

    ctx.beginPath();
    ctx.moveTo(x + radius, y);
    ctx.lineTo(x + width - radius, y);
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
    ctx.lineTo(x + width, y + height - radius);
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
    ctx.lineTo(x + radius, y + height);
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
    ctx.lineTo(x, y + radius);
    ctx.quadraticCurveTo(x, y, x + radius, y);
    ctx.closePath();
    ctx.strokeStyle= "white";
    ctx.stroke();
    ctx.fillStyle = "black";
    ctx.fill();
    ctx.font = "30px Arial";
    ctx.fillStyle = "white";

    if(turn < 5) {
        for (var i = 0; i < party[turn-1].menu.length; i++) {
            ctx.fillText(party[turn-1].menu[i], (canvas.width / 2) + 20, ((canvas.height / 5) * 4) + (35 * (i + 0.50)));
        }
    }
    if(!isSelecting){
        if(fightMenu == 1){
            ctx.drawImage(cursor,(canvas.width / 2) + 5 , ((canvas.height / 5) * 4) +(35 * (0.50)));
        }
        else if(fightMenu == 2){
            ctx.drawImage(cursor,(canvas.width / 2) + 5 ,((canvas.height / 5) * 4) + (35 * (1 + 0.50)));
        }
        else if(fightMenu == 3){
            ctx.drawImage(cursor,(canvas.width / 2) + 5 ,((canvas.height / 5) * 4) + (35 * (2 + 0.50)));
        }

    }
    else {
        if(targetingMonster){
            if(monsterTarget == 1){
                ctx.drawImage(cursor,50,100);
            }
            else if(monsterTarget == 2){
                ctx.drawImage(cursor,50,150);
            }
            else if(monsterTarget == 3){
                ctx.drawImage(cursor,50,200);
            }

        }

        if(targetingAlly){
                if(allyTarget == 1){
                    ctx.drawImage(cursor,385,100);
                }
                else if(allyTarget == 2){
                    ctx.drawImage(cursor,385,180);
                }
                else if(allyTarget == 3){
                    ctx.drawImage(cursor,385,260);
                }
                else if(allyTarget == 4){
                    ctx.drawImage(cursor,385,340);
                }
            }
        }
    }



function drawRectEnnemies(){
    ctx.rect(0,((canvas.height/5)*4),(canvas.width/2),(canvas.height -((canvas.height/5)*4)));
    ctx.strokeStyle= "white";
    ctx.stroke();
    ctx.font = "20px Arial";
    ctx.fillStyle = "white";

    for(var i = 0 ; i < monstersBattle.length ; i++) {

        ctx.fillText(monstersBattle[i].name,0+20,((canvas.height/5)*4)+(35*(i+0.25)));
    }

}

function generateRandomEnnemies(){
    var nbOfEnnemies = Math.random()*3;
    var monster;
    for(var i = 0 ; i < nbOfEnnemies ; i++){
        var ennemiesType = Math.random()*2;
        if(ennemiesType = 0){
            monster = new Goblin();
        }
        else{
            monster = new Warg();
        }
        monstersBattle.push(monster);
    }

}


//
var mapArray = [
    [0, 0, 0, 0 ,0],
    [0, 1, 0, 0 ,0],
    [0, 0, 0, 0 ,0],
    [0, 0, 0, 0 ,0],
    [0, 0, 1, 1 ,0]
];

//ctx.drawImage(grass,0,0),20,20,,,40,40);
function drawMap() {
    background = new createjs.Container();
    for (var y = 0; y < mapArray.length; y++) {
        for (var x = 0; x < mapArray[y].length; x++) {
            if (parseInt(mapArray[y][x]) == 0) {
                var tile = new createjs.Bitmap('images/tile.png');
            }
            if (parseInt(mapArray[y][x]) == 1) {
                var tile = new createjs.Bitmap('images/tile2.png');
            }
            tile.x = x * 28;
            tile.y = y * 28;

        }
    }
    stage.addChild(background);
}
/**
 * Created by michael on 2015-02-23.
 */
